﻿using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Reflection.Metadata;
using System.Text;
using System.Threading.Tasks;

namespace TestConsole
{
    public static class HttpClientExtensions
    {
        public static async Task<string> SendRequestAsync(this HttpClient client, Action<HttpRequestMessage> reqAction = null)
        {
            if (client == null)
            {
                throw new ArgumentNullException("client can not be null");
            }

            using (client)
            {
                var req = new HttpRequestMessage
                {
                    Method = HttpMethod.Get
                };

                reqAction?.Invoke(req);
                var response = await client.SendAsync(req);
                if (response.IsSuccessStatusCode)
                {
                    return await response.Content.ReadAsStringAsync();
                }
                else
                {
                    return await Task.FromResult(string.Empty);
                }
            }
        }
    }
}
